// 导入three.js
import * as THREE from "three";
// 导入轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
// 导入GUI
import { GUI } from "three/examples/jsm/libs/lil-gui.module.min.js"
// 导入hdr加载器
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader.js"
// 导入gltf加载器
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js"
// 导入draco解码器
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader.js"
// 导入补间动画tween库
import * as TWEEN from "three/examples/jsm/libs/tween.module.js"
// 导入顶点法向量的辅助器
import { VertexNormalsHelper } from "three/examples/jsm/helpers/VertexNormalsHelper.js"

// 创建场景
const scene = new THREE.Scene()
// scene.background = new THREE.Color("#c6c6c6")
// 创建相机
const camera = new THREE.PerspectiveCamera(
  45, // 视角
  window.innerWidth / window.innerHeight, //宽高比
  0.1, // 近平面
  1000 // 远平面
)

// 创建渲染器
const renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight) // 设置渲染器宽高
document.body.appendChild(renderer.domElement) // 将渲染器添加到body中

// 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true // 开启阻尼
controls.dampingFactor = 0.05 // 阻尼系数

// 设置相机位置
camera.position.z = 15
camera.position.y = 4
camera.position.x = 2
camera.lookAt(0, 0, 0)

// 添加世界辅助坐标器
const axisHelper = new THREE.AxesHelper(5)
scene.add(axisHelper)

function init() {
  controls.update() // 更新控制器
  requestAnimationFrame(init);
  renderer.render(scene, camera) // 渲染场景和相机
  // 更新tween
  TWEEN.update()
}

init() //初始化
// 创建GUI
const gui = new GUI()

// 加载UV贴图
const uvTexture = new THREE.TextureLoader().load("./image/UVtest.png")

// 实例化rgbload
const hdrloader = new RGBELoader()
hdrloader.load("./image/bloem_train_track_clear_4k.hdr", function (envMap) {
  // 设置环境贴图
  scene.background = envMap
  // 设置球型映射
  envMap.mapping = THREE.EquirectangularReflectionMapping
  // 设置环境贴图
  scene.environment = envMap
  // 设置plane环境贴图
  // sphereMaterial.envMap = envMap
  // planeMaterial.needsUpdate = true
})

// 创建一个球体
const sphere = new THREE.SphereGeometry(1, 32, 32)
const sphereMaterial = new THREE.MeshStandardMaterial({
    color: 0x00ffff,
  })
const cubeSphere = new THREE.Mesh(sphere, sphereMaterial)
scene.add(cubeSphere)

const SphereGeometry = cubeSphere.geometry
// 计算包围盒
SphereGeometry.computeBoundingBox()
// 获取包围盒
const sphereBox = SphereGeometry.boundingBox

// // 更新世界矩阵
// cubeSphere.updateWorldMatrix(true, true)
// // 更新包围盒
// sphereBox.applyMatrix4(cubeSphere.matrixWorld)

console.log(sphereBox)
// 创建包围盒辅助器
const boxHelper = new THREE.BoxHelper(cubeSphere, 0xffff00)
scene.add(boxHelper)